package com.leyou.item.service;

import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 后台管理系统分类管理,根据pid查询分类
     *
     * @param pid
     * @return
     */
    public List<Category> queryListByParent(Long pid) {

        Category category = new Category();
        category.setParentId(pid);

        return this.categoryMapper.select(category);
    }


    /**
     * 品牌管理界面 修改品牌弹窗数据回显
     *
     * @param bid
     * @return
     */
    public List<Category> queryByBrandId(Long bid) {

        return this.categoryMapper.queryByBrandId(bid);

    }


    /**
     *查询商品分类的名字
     * @param ids
     * @return
     *
     * public List<String> queryNameByIds(List<Long> ids) {
     *         return this.categoryMapper.selectByIdList(ids).stream()
     *                 .map(Category::getName).collect(Collectors.toList());
     * }
     *  TODO 上面这个方法有空研究一下
     * 效果相同
     */
    public List<String> queryNamesByIds(List<Long> ids) {
        List<Category> categories = this.categoryMapper.selectByIdList(ids);
        List<String> names = new ArrayList<>();
        categories.forEach(category -> names.add(category.getName()));
        return names;
    }


    public List<Category> queryAllByCId3(Long cid) {

//        Category category3 = this.categoryMapper.selectByPrimaryKey(cid);
//        Category category2 = this.categoryMapper.selectByPrimaryKey(category3.getParentId());
//        Category category1 = this.categoryMapper.selectByPrimaryKey(category2.getParentId());
        List<Category> list = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            Category category = this.categoryMapper.selectByPrimaryKey(cid);
            list.add(category);
            if (category.getParentId() == 0) break;
            cid = category.getParentId();
        }
        return list;
    }
}
